﻿Public Class frmBackup


    Private Sub frmBackup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        lbBackupComment.Text = "* 백업본은 " + gBackupTerms.ToString + "일 보관되며 " + gBackupTerms.ToString + "일이후 부터는 순차적으로 "
        Call listup()
    End Sub

    Private Sub btnBackup_Click(sender As System.Object, e As System.EventArgs) Handles btnBackup.Click
        Call gSubDataBackup("백업이 완료 되었습니다.")
        Call listup()
    End Sub

    Private Sub btnRestore_Click(sender As System.Object, e As System.EventArgs) Handles btnRestore.Click

        If listBackup.SelectedIndex.ToString() = -1 Then
            MsgBox("복구할 데이터를 선택해 주세요")
            Exit Sub
        End If

        If listBackup.SelectedItems.Count > 1 Then
            MsgBox("복구할 데이터를 하나만 선택해 주세요")
            Exit Sub
        End If
        '## 현재 파일 백업
        Call gSubDataBackup("")
        '## 복구 실행
        Dim result As Integer = MessageBox.Show("선택한 파일을 복구 하시겠습니까?", "InventoryMan", MessageBoxButtons.YesNo)
        If result = DialogResult.No Then
            Exit Sub
        ElseIf result = DialogResult.Yes Then
            Call dataRestore(listBackup.SelectedItem.ToString, "복구가 완료 되었습니다.")
        End If
    End Sub

    Private Sub btnRefresh_Click(sender As System.Object, e As System.EventArgs) Handles btnRefresh.Click
        Call listup()
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Dispose()
    End Sub

#Region "프로시져"

    Private Sub listup()
        listBackup.Items.Clear()
        Dim di As New IO.DirectoryInfo(gBackupDir)
        Dim diFiles As IO.FileInfo() = di.GetFiles()

        If (Not System.IO.Directory.Exists(gBackupDir)) Then
            System.IO.Directory.CreateDirectory(gBackupDir)
        End If

        Dim Files() As String = IO.Directory.GetFiles(gBackupDir).Concat(IO.Directory.GetFiles(gBackupDir)).ToArray
        Array.Reverse(Files)

        listBackup.Items.Clear()
        'list the names of all files in the specified directory

        For Each file As String In Files
            listBackup.Items.Add(System.IO.Path.GetFileName(file))
        Next
    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        Dim deleteFile As String
        If listBackup.SelectedIndex.ToString() = -1 Then
            MsgBox("삭제할 데이터를 선택해 주세요")
            Exit Sub
        End If

        Dim result As Integer = MessageBox.Show("선택한 파일을 삭제 하시겠습니까?", "InventoryMan", MessageBoxButtons.YesNo)
        For k = 0 To listBackup.SelectedItems.Count - 1
            deleteFile = gBackupDir + "/" + listBackup.SelectedItems(k).ToString
            If result = DialogResult.No Then
                Exit Sub
            ElseIf result = DialogResult.Yes Then
                If System.IO.File.Exists(deleteFile) = True Then
                    System.IO.File.Delete(deleteFile)
                End If
            End If
        Next
        Call listup()
    End Sub

    Private Sub dataRestore(ByVal src As String, ByVal msg As String)
        Dim srcFile, targetFile As String

        'targetFile = Application.StartupPath + "/data/dbInventory.MDB"
        targetFile = gDataFile
        srcFile = gBackupDir + "/" + src

        If System.IO.File.Exists(targetFile) = True Then
            System.IO.File.Delete(targetFile)
        End If

        If System.IO.File.Exists(srcFile) = True Then
            System.IO.File.Copy(srcFile, targetFile)
        End If
        If msg <> "" Then
            MsgBox(msg)
        End If
    End Sub
#End Region

    Private Sub listBackup_DoubleClick(sender As Object, e As System.EventArgs) Handles listBackup.DoubleClick
        Dim result As Integer = MessageBox.Show("선택한 파일을 복구 하시겠습니까?", "InventoryMan", MessageBoxButtons.YesNo)
        If result = DialogResult.No Then
            Exit Sub
        ElseIf result = DialogResult.Yes Then
            Call dataRestore(listBackup.SelectedItem.ToString, "복구가 완료 되었습니다.")
        End If
    End Sub
End Class